package com.caac.core.web.resmgr.bean;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Stack;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright 2004</p>
 * <p>Company: </p>
 * @author <a href="mailto:lianzs@caac.com.cn">lianzs</a>
 * @version 1.0
 */
public class Directory {

    private final static String initAction = "initDirectory.do?dir=";

    private String name;
    public Directory[] subDirectories;
    private int nodeIndex;
    private Stack parentNodes = new Stack();
    private ArrayList curNodeParentPath = new ArrayList();
    private java.util.Locale displayLocale = Locale.CHINESE;

    public Directory() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // Delete the directory.
    public static void delete(String dirName) {
        File path = new File(dirName);
        File[] list = path.listFiles();
        if (list == null) {
            return;
        }

        ArrayList subDirs = new ArrayList();
        for (int i = 0; i < list.length; i++) {
            if (list[i].isDirectory()) {
                subDirs.add(list[i].getAbsolutePath());
            } else {
                list[i].delete();
            }
        }

        Iterator it = subDirs.iterator();
        while (it.hasNext()) {
            delete((String) it.next());
        }

        // Delete this
        path.delete();
    }

    public String getShortName() {
        return getShortName(this.name);
    }

    public static String getShortName(String name) {
        String fileSeperator = System.getProperty("file.separator");
        int lastIndex = name.lastIndexOf(fileSeperator);

        return name.substring(lastIndex + 1);
    }

    public Directory[] getSubDirectories() {
        return subDirectories;
    }

    public void setSubDirectories(Directory[] subDirectories) {
        this.subDirectories = subDirectories;
    }

    public void print() {
        print(this);
    }

    public void print(Directory dir) {
        System.out.println(dir.getName());
        System.out.println(dir.getShortName());
        for (int i = 0; i < dir.subDirectories.length; i++) {
            print(dir.subDirectories[i]);
        }
    }

    public String getJavaScript(String openedDirectory) throws
            UnsupportedEncodingException {
        StringBuffer script = new StringBuffer();
        String rootNodeName = "Root";

        ResourceBundle rb = ResourceBundle.getBundle(
                "com.caac.core.web.resource.ResMgr", displayLocale);
        String rootname = rb.getString("ResMgr.Root");
        script.append(" Root= createDirectory(\"<b>" + rootname + "</b>\", '"
                      + initAction
                      + SelfEncoder.encodeString(this.name)
                      + "')\r\n");
        this.nodeIndex = 0;

        // Make sub directories node script and call this function recursive.
        script.append(this.getJavaScript(this, openedDirectory, rootNodeName));
        return script.toString();
    }

    public String getJavaScript(
            Directory dir,
            String openedDirectory,
            String rootNodeName) {
        StringBuffer script = new StringBuffer();
        for (int i = 0; i < dir.subDirectories.length; i++) {
            String thisNodeName = rootNodeName + Integer.toString(i);
            String thisNodePicture;
            if (dir.subDirectories[i].getName().equals(openedDirectory)) {
                thisNodePicture =
                        "<img src=/wb/resmgr/images/o.gif border=0>";
                this.curNodeParentPath = new ArrayList(parentNodes);
                this.curNodeParentPath.add(new Integer(nodeIndex + 1));
            } else {
                thisNodePicture =
                        "<img src=/wb/resmgr/images/c.gif border=0>";
            }

            // Add this node.
            String tmp = SelfEncoder.encodeString(dir.subDirectories[i].getName());
            script.append(thisNodeName
                          + " = insertDirectory(" + rootNodeName
                          + ", createDirectory('" + thisNodePicture
                          + dir.subDirectories[i].getShortName() + "', '"
                          + initAction + tmp
                          + "'))\r\n");

            nodeIndex++;
            parentNodes.push(new Integer(nodeIndex));

            // Call recursive.
            script.append(this.getJavaScript(dir.subDirectories[i],
                                             openedDirectory, thisNodeName));
            parentNodes.pop();
        }
        return script.toString();
    }

    public ArrayList getCurNodeParentPath() {
        return this.curNodeParentPath;
    }

    public java.util.Locale getDisplayLocale() {
        return displayLocale;
    }

    public void setDisplayLocale(java.util.Locale displayLocale) {
        this.displayLocale = displayLocale;
    }
}
